﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Globalization

Namespace System.Workflow.ComponentModel
    Friend NotInheritable Class SynchronizationHandlesTypeConverter
        Inherits TypeConverter
        ' Methods
        Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            Return ((sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
        End Function

        Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
            Return ((destinationType Is GetType(String)) OrElse MyBase.CanConvertTo(context, destinationType))
        End Function

        Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
            If TypeOf value Is String Then
                Return SynchronizationHandlesTypeConverter.UnStringify([TryCast](value,String))
            End If
            Return MyBase.ConvertFrom(context, culture, value)
        End Function

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            If ((destinationType Is GetType(String)) AndAlso TypeOf value Is ICollection([Of] String)) Then
                Return SynchronizationHandlesTypeConverter.Stringify([TryCast](value,ICollection([Of] String)))
            End If
            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function

        Friend Shared Function Stringify(ByVal synchronizationHandles As ICollection([Of] String)) As String
            Dim str As String = String.Empty
            If (Not synchronizationHandles Is Nothing) Then
                Dim str2 As String
                For Each str2 In synchronizationHandles
                    If (Not str2 Is Nothing) Then
                        If (str <> String.Empty) Then
                            str = (str & ", ")
                        End If
                        str = (str & str2.Replace(",", "\,"))
                    End If
                Next
            End If
            Return str
        End Function

        Friend Shared Function UnStringify(ByVal stringifiedValue As String) As ICollection([Of] String)
            Dim is2 As ICollection([Of] String) = New List([Of] String)
            stringifiedValue = stringifiedValue.Replace("\,", ">")
            Dim str As String
            For Each str In stringifiedValue.Split(New Char() { ","c, ChrW(13), ChrW(10) }, StringSplitOptions.RemoveEmptyEntries)
                Dim item As String = str.Trim.Replace(">"c, ","c)
                If ((item <> String.Empty) AndAlso Not is2.Contains(item)) Then
                    is2.Add(item)
                End If
            Next
            Return is2
        End Function

    End Class
End Namespace

